
# baidu ocr
from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '25071189'
API_KEY = 'uoTYdBZQIb4eIpldm7w87Q3D'
SECRET_KEY = '9aW5bxWGXgnimCoFh86nN31r5klL9FPz'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

def get_sfz_id(image_path):
    image = image_path

    idCardSide = "front"

    """ 调用身份证识别 """
    client.idcard(image, idCardSide);

    """ 如果有可选参数 """
    options = {}
    options["detect_direction"] = "true"
    options["detect_risk"] = "false"

    """ 带参数调用身份证识别 """
    response = client.idcard(image, idCardSide, options)
    if response["image_status"] == 'non_idcard':
        return False,"请拍摄身份证"
    try:
        sfzid = response["words_result"]["公民身份号码"]["words"]
        name = response["words_result"]["姓名"]["words"]
        return True,{
            "sfzid":sfzid,
            "name":name
        }
    except KeyError:
        return False,"识别失败，请手动填入身份证号"
